//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <elastos.h>
#include <elapi.h>
#include <bootmods.h>
#include <stdlib.h>
#include <_pubcrt.h>

_ELASTOS_NAMESPACE_USING

ECode __stdcall CrtGetBootModule(const wchar_t *wszName,
    UInt32 *puAddr, UInt32 *puSize)
{
    IDeviceDriver *pDriver;
    MemoryBuf_<64> ebbInfo;
    MemoryBuf_<_MAX_PATH> ebbFileName;

    char szName[_MAX_PATH];
    _wcstostr(szName, wszName);
    ebbFileName.Copy((Byte *)szName, strlen(szName) + 1);

    // Get the IBasicFileSystem pointer
    ECode ec = _CSystem_FindRunningObject(L"device:bootmods0",
        (PInterface *)&pDriver);
    if (FAILED(ec)) {
        return ec;
    }

    ec = pDriver->Control(BMCommand_Open, ebbFileName, &ebbInfo, NULL);
    pDriver->Release();
    if (FAILED(ec)) {
        return ec;
    }

    BMFileInfo *pInfo = (BMFileInfo *)ebbInfo.GetPayload();
    if (!pInfo->uSize) return E_DOES_NOT_EXIST;

    *puAddr = pInfo->uUAddr;
    *puSize = pInfo->uSize;
    return NOERROR;
}

ELAPI  _EzGetEnvironmentMutex(IMutex **ppEnvMutex);

extern "C" IMutex *g_pEnvMutex;
IMutex *g_pEnvMutex = NULL;
extern "C" IMutex ** CDECL __p__g_pEnvMutex(void)
{
    return &g_pEnvMutex;
}

extern "C"IMutex * __cdecl EnvironMutex()
{
    if (NULL == g_pEnvMutex) {
        _EzGetEnvironmentMutex(&g_pEnvMutex);
        assert(g_pEnvMutex);
    }
    return g_pEnvMutex;
}
